import {
  getPage
} from '../../utils/config';
import api from '../../utils/api';
import wxApi from '../../utils/wxApi';
import {
  http,
  extend,
  promisify,
  $loading,
  $toast
} from '../../utils/utils';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    moveState: true,
    loading: 0,
    logged: 0,
    pageSize: 20,
    userId: '0',
    loadmore: false
  },


  /**
   * 下拉--刷新数据
   */
  handleToUpper(e) {
    
    $loading();

    let orderInfo = {
      page: 1,
      abnor: false,
      load: false,
      list: [],
      nextPages: true,
    };

    this.setData({
      orderInfo
    });

    this.getOrderInfo((res) => {
      orderInfo.list = res.list;
      this.setData({
        orderInfo
      });

      setTimeout(() => {
        this[`scroll`].stop();
        wx.hideLoading();
      }, 300)
    });
  },

  /**
   * 上拉--加载数据
   */
  handleTolower() {
    const {
      loadmore,
      orderInfo
    } = this.data;

    if (orderInfo.nextPages && !loadmore) {
      orderInfo.page++;
      this.setData({
        orderInfo,
        loadmore: true
      });

      this.getOrderInfo((res) => {
        orderInfo.list = orderInfo.list.concat(res.list);
        this.setData({
          orderInfo,
          loadmore: false
        });
      });
    }
  },
  addressChange(addressid, orderId) {
    const {
      userId
    } = this.data;
    http.post(api.default, {
      data: {
        action: 'updateprizeaddress',
        orderid: orderId,
        userid: userId,
        addressid: addressid,
      }
    }).then((res) => {
      // console.log("addressChange=======data=", res.data.data)
      // console.log(res.data.code)
      if (res.data.code == 0) {
        //成功提示
        //console.log('成功')
        //获取当前订单最新状态
        this.replaceOrder(orderId, res.data.data);
        $toast(`奖品领取成功！`, {
          icon: 'success'
        });


      } else {
        $toast(res.data.errmsg, {
          icon: 'error'
        });
      }
    })
  },

  /**
   * 替换--操作数据
   * @parems {Array} a 需要操作的对象下标
   * @parems {Number} id 需要匹配的对象id
   * @parems {Object} data 需要替换的对象数据
   */
  replaceOrder(id, data) {
    const {
      orderInfo
    } = this.data;
    id = Number(id);
    const index = orderInfo.list.map(item => item.id).indexOf(id);
    orderInfo.list[index] = data;
    console.log('replaceOrder=========orderInfo=', orderInfo);
    this.setData({
      orderInfo
    });
  },

  /**
   * 获取订单--网络请求
   */
  getOrderInfo(cb, n) {
    let {
      userId,
      pageSize,
      orderInfo
    } = this.data;
    http.get(api.default, {
      data: {
        action: 'getmyprizes',
        userid: userId,
        page: orderInfo.page,
        pageSize: pageSize,
      }
    }, false).then((res) => {
      const {
        data
      } = res.data;

      //判断是否已加载
      if (!orderInfo.load) {
        orderInfo.load = true;
      }

      //判断是否没有数据
      if (data.list.length == 0) {
        orderInfo.abnor = true;
      }

      //判断是否还有下一页
      if (data.list.length < pageSize) {
        orderInfo.nextPages = false;
      }
      cb && cb(data);
      this.setData({
        orderInfo
      });
    })
  },


  /**
   * 查看物流--路由跳转
   */
  jumpShippingClick(e) {
    const {
      dataset
    } = e.currentTarget || e.target;
    wxApi.navigateTo(getPage.orderExpress, {
      orderId: dataset.orderId,
      tracknum: dataset.tracknum,
      shippingName: dataset.shippingName,
      shipping: dataset.shipping,
    });
  },

  /**
   * 查看物流--路由跳转
   */
  jumpAddressClick(e) {
    const {
      dataset
    } = e.currentTarget || e.target;
    wxApi.navigateTo(getPage.address, {
      mode: 1,
      orderid: dataset.id
    });
  },
  copytracknum(e) {
    const {
      dataset
    } = e.currentTarget || e.target;
    wx.setClipboardData({
      data: dataset.tracknum,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
          icon: 'none' // 可以选择显示或隐藏图标
        });
      },
      fail: function (res) {
        wx.showToast({
          title: '复制失败',
          icon: 'none' // 可以选择显示或隐藏图标
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    //判断入口是否带参数
    if (options && JSON.stringify(options) !== '{}') {

    }

    //判断设备
    if (wx.getSystemInfoSync().system.indexOf('Android') !== -1) {
      this.setData({
        pageSize: 10
      });
    };

    //判断是否登录
    app.getLogin().then((res) => {
      const {
        user
      } = res.data;

      this.setData({
        logged: res.code,
        userId: user.user_id,
        userRank: user.user_rank,
      });

      //创建数据模型
      let orderInfo = {
        page: 1,
        abnor: false,
        load: false,
        list: [],
        nextPages: true,
      };
      this.setData({
        orderInfo
      });

      this.getOrderInfo((res) => {
        orderInfo.list = res.list;
        this.setData({
          orderInfo,
          loading: 1
        });
        //console.log('orderInfo=', orderInfo)
      });

      this[`scroll`] = this.selectComponent(`#scroll`);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
})